const wx = (function () {
    const wx = {};

    wx.open_loading = false;

    wx.loading = function (loading_text) {
        if (!wx.open_loading) {
            return {
                hide(callback) {
                    callback && callback();
                }
            };
        }

        return weui.loading(loading_text || "加载中");
    };

    let loader = function (callback) {
        let loading = wx.loading("加载中");

        util.require(['wx'], function (wxsdk) {
            loading.hide(function () {
                callback && callback(wxsdk);
            });
        }, function (err) {
            console.error("微信功能加载错误，可能是未加载jssdk(jweixin)导致");
        });
    }

    wx.init = function (access, options, callback) {
        options = Object.assign({
            apiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareWeibo',
                'onMenuShareQZone',
                'hideMenuItems',
                'showMenuItems',
                'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem',
                'translateVoice',
                'startRecord',
                'stopRecord',
                'onVoiceRecordEnd',
                'playVoice',
                'onVoicePlayEnd',
                'pauseVoice',
                'stopVoice',
                'uploadVoice',
                'downloadVoice',
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'getLocalImgData',
                'getNetworkType',
                'openLocation',
                'getLocation',
                'hideOptionMenu',
                'showOptionMenu',
                'closeWindow',
                'scanQRCode',
                'chooseWXPay',
                'openProductSpecificView',
            ]
        }, options);

        loader(function (wxsdk) {
            wxsdk.hideAllNonBaseMenuItem();
            wxsdk.showMenuItems({
                menuList: ['menuItem:copyUrl']
            });
            wxsdk.config({
                debug: false,
                appId: access.appId,
                timestamp: access.timestamp,
                nonceStr: access.nonceStr,
                signature: access.signature,
                jsApiList: options.apiList
            });

            callback && callback(wxsdk);
        });
    }

    wx.uploadImage = function (options={}, callback) {
        options = Object.assign({
            count: 9,
            sizeType: ['compressed'], // 可以指定是原图original还是压缩图compressed，默认二者都有
            sourceType: ['album'], // 可以指定来源是相册album还是相机camera，默认二者都有
        }, options);

        let local2base64 = function (localIds, callback, index=0, files=[]) {
            let localId = localIds[index];

            if (util.isEmpty(localId)) {
                callback && callback(files);
                return;
            }

            loader(function (wxsdk) {
                wxsdk.getLocalImgData({
                    localId: localId,
                    success: function (res) {
                        let imagebase64 = res.localData;
                        let imageblob = util.base642Blob(imagebase64);
                        let file = util.blob2File(imageblob, "wx_"+ util.getCalendarDate().timestamp + index +"."+ util.defaults(util.defaults(imageblob.type, "").split('/')[1], "jpg"), {type: imageblob.type});
                        files.push(file);
                        local2base64(localIds, callback, index+1, files);
                    }
                });
            });
        }

        options.success = function (res) {
            local2base64(res.localIds, function (files) {
                callback && callback(files);
            });
        }

        loader(function (wxsdk) {
            wxsdk.chooseImage(options);
        });
    }

    return wx;
})();